import { getSubjectsListApi, majorsListApi } from '@/api/table'
import request from '@/utils/request'

// 根据key和页数获取数据下标
export function getSortIndex(key, listData, page) {
  for (const i in listData[page - 1]) {
    if (listData[page - 1][i].key === key) {
      return i
    }
  }
}

export function pageSizeChange(page) {
  const yy = []
  for (const y in this.listData) {
    this.listData[y].forEach(r => {
      yy.push(r)
    })
  }
  const { newListData, data } = tableDataProcess(yy, page)
  this.pageConfig.total = data.length
  this.listData = newListData
}
// 删除数据接口
export function deleteData(API, data) {
  request({
    url: API,
    method: 'post',
    data: data
  }).then(response => {
    this.$message({
      message: response.data.msg,
      type: 'success'
    })
    this.getListData()
  }).catch(() => {
  })
}
export function Formatter(row) {
  const date = new Date(row)
  const Y = date.getFullYear()
  const M =
        date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1
  const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  const h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  const m =
        date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  const s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
}
// 表单数据分页
export function tableDataProcess(data, pagesSize) {
  data = data.sort((a, b) => {
    a.status = !!a.status
    b.status = !!b.status
    return a.key - b.key
  })

  // 分隔页
  const newListData = []
  for (let i = 0; i < Math.ceil(data.length / pagesSize); i++) {
    newListData.push(data.slice(i * pagesSize, i * pagesSize + pagesSize))
  }
  return { newListData, data }
}

export function getSubjectOption() {
  const subjectOption = []
  const majorsList = []
  // 获取专业表
  majorsListApi().then((res) => {
    // 序列化专业表
    for (const i in res.data.data) {
      res.data.data[i].status = !res.data.data[i].status
      subjectOption.push({
        value: res.data.data[i].major_name,
        label: res.data.data[i].major_name,
        disabled: res.data.data[i].status,
        children: []
      })
      const { status, major_name } = res.data.data[i]
      majorsList.push({
        major_name: major_name,
        status: status
      })
    }
    // 获取科目表
    getSubjectsListApi().then((res) => {
      const subjectList = []
      // 根据专业序列化科目选项
      for (const i in res.data.data) {
        res.data.data[i].status = !res.data.data[i].status
        subjectList.push({
          major_name: res.data.data[i].major_name,
          subject_name: res.data.data[i].subject_name,
          status: res.data.data[i].status
        })
      }
      for (const j in subjectOption) {
        for (const l in subjectList) {
          if (subjectList[l].major_name === subjectOption[j].label) {
            subjectOption[j].children.push({
              label: subjectList[l].subject_name,
              value: subjectList[l].subject_name,
              disabled: subjectList[l].status
            })
          }
        }
      }
    })
  })
  return subjectOption
}
